#include "../import/head.h"
#include "protocol_heart.h"
#include "../helper_package/head.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"

namespace xos_sgsj
{

    protocol_heart::protocol_heart()
    {
    }

    protocol_heart::~protocol_heart()
    {
    }

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//

	int protocol_heart::dis_compose( session * pSession, xos::i_buf * pBuf )
	{
		int ret = 0;

		char buf[4096] = { 0 };
		int len = 0;

		if( 0 == ret )
		{
			ret = g_pSysData->m_os.m_pSerial->un_pack_mem( pBuf, buf, sizeof( buf ), &len, 0 );
		}

		if( 0 == ret )
		{
			LOG4( "recv heart = %s", buf );
		}

		return ret;
	}

	int protocol_heart::compose( tcp * pTcp )
	{
		int ret = 0;

		xos::i_time * pTime = 0;
		xos::i_buf * pBuf = 0;

		if( 0 == ret )
		{
			ret = g_pSysData->m_os.m_pXos->create( xos::i_xos::XOS_OBJ_TIME, (void **)&pTime );
		}

		if( 0 == ret )
		{
			ret = g_pSysData->m_os.m_pXos->create( xos::i_xos::XOS_OBJ_BUF, (void **)&pBuf );
		}

		if( 0 == ret )
		{
			ret = g_pSysData->m_os.m_pSerial->pack_int8( pBuf, PROTOCOL_HEART );
		}

		if( 0 == ret )
		{
			char buf[4096] = { 0 };
			int len = 0;
			pTime->get_time();
			len = g_pSysData->m_os.m_pCrt->sprintf( buf, sizeof( buf ), "%04d-%02d-%02d %02d:%02d:%02d", 
				pTime->get_year(),
				pTime->get_month(), 
				pTime->get_day(), 
				pTime->get_hour(), 
				pTime->get_minute(), 
				pTime->get_second());
			ret = g_pSysData->m_os.m_pSerial->pack_mem( pBuf, buf, len );
			LOG4( "send heart = %s", buf );
		}

		if( 0 == ret )
		{
			helper_package obj;
			ret = obj.post_package( pTcp->m_pSession, pBuf );
			pBuf = 0;
		}

		if( pTime )
		{
			pTime->release();
			pTime = 0;
		}

		if( pBuf )
		{
			pBuf->release();
			pBuf = 0;
		}

		return ret;
	}

} // xos_sgsj
